﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;

using SIPO.Library.DomainObject.Administration.UserManagement;
using SIPO.Library.DataAccess.Administration.UserManagement;

namespace SIPO.Web.Support
{
    public class Membership
    {
        private recUserRepo userDal = null;

        private string SIPOConnectionString = string.Empty;

        public Membership(string connectionString)
        {
            SIPOConnectionString = connectionString;
        }

        public bool ValidateUser(string usercode, string password, out recUser user)
        {
            userDal = new recUserRepo(SIPOConnectionString);

            return userDal.ValidateUser(usercode, password, out user);

        }

        public List<recMenu> GetMenusByRoleIDs(List<int> roleIds)
        {
            recMenuRightsRepo menuRightsDal = new recMenuRightsRepo(SIPOConnectionString);

            var menuIds = (from m in menuRightsDal.GetList()
                           where (roleIds.Contains(m.RoleID))
                           select m.MenuID).Distinct();

            recMenuRepo menuDal = new recMenuRepo(SIPOConnectionString);

            List<recMenu> menus = menuDal.GetList().Where(m => menuIds.Contains(m.MenuID)).ToList();

            return menus;

        }

        public List<recUserRole> GetRoles(int UserID)
        {
            recUserRoleRepo userRoleDal = new recUserRoleRepo(SIPOConnectionString);
            List<recUserRole> userRoles = userRoleDal.GetListByUserID(UserID);

            return userRoles;

        }

        public bool IsAdmin(int UserID)
        {
            bool result = false;

            recRoleRepo roleDal = new recRoleRepo(SIPOConnectionString);

            recUserRoleRepo userRoleDal = new recUserRoleRepo(SIPOConnectionString);

            var role = roleDal.GetList().Find(x => x.RoleName.ToLower().Trim() == "administrator");

            if (role != null)
            {
                userRoleDal.GetList().Where(x => x.UserID == UserID);
            }

            return result;
        }

        public List<recAccessRights.AccessRights> GetAccessRights(int userId, int menuId)
        {
            List<recUserRole> roles = GetRoles(userId); // Get All Roles from User
            List<recAccessRights.AccessRights> results = new List<recAccessRights.AccessRights>();

            recMenuRightsRepo menuRightsDal = new recMenuRightsRepo(SIPOConnectionString);

            bool viewAble = false;
            roles.ForEach(delegate(recUserRole role)
            {
                List<recAccessRights.AccessRights> accessRights = menuRightsDal.GetAccessRights(role.RoleID.Value, menuId);
                accessRights.ForEach(delegate(recAccessRights.AccessRights right)
                {
                    if (!results.Contains(right))
                    {
                        results.Add(right);
                    }

                    if (right == recAccessRights.AccessRights.View)
                        viewAble = true;

                });

            });

            if (!viewAble) // if user has no access view then add "NoAccess" right
                results.Add(recAccessRights.AccessRights.NoAccess);

            return results;

        }

        public string CreatePassword(int length)
        {
            string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
            string res = "";
            Random rnd = new Random();
            while (0 < length--)
                res += valid[rnd.Next(valid.Length)];
            return res;
        }

    }

}